package com.lisn.recyclerviewlib;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RecyclerViewTestActivity extends AppCompatActivity {
    private static final String TAG = RecyclerViewTestActivity.class.getSimpleName();
    private Context context;
    private RecyclerView recyclerView;
    private Button delete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view_test);
        context = RecyclerViewTestActivity.this;
        recyclerView = findViewById(R.id.recyclerView);
        delete = findViewById(R.id.bt_delete);
        init();
    }

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, RecyclerViewTestActivity.class));
    }


    public void init() {
        final List<Integer> deleteList = new ArrayList<>();//定义list存储要删除的数
        //实现recyclerview加载
        LinearLayoutManager manager = new LinearLayoutManager(context, RecyclerView.VERTICAL, false);

//        GridLayoutManager manager = new GridLayoutManager(context, 3);
        recyclerView.setLayoutManager(manager);
        List<Notes> list = new ArrayList<>();
        for (int i = 0; i < 150; i++) {
            list.add(new Notes(i));
        }
        final NotesAdapter adapter = new NotesAdapter(list, context);
        recyclerView.setAdapter(adapter);
        //实现长按事件
        adapter.setOnItemLongClickListener(new NotesAdapter.OnItemLongClickListener() {
            @Override
            public void OnItemLongClick(Boolean longclick) {
                //长按后底部出现的删除按钮
                if (!longclick) {
                    delete.setVisibility(View.GONE);
                    deleteList.removeAll(deleteList);//清空表（完成一次删除后立刻清空list表）
                } else {
                    delete.setVisibility(View.VISIBLE);
                }
                adapter.notifyDataSetChanged();//更新adapter目的是当长按一个item后所有的item都进入长按状态
            }
        });
        //实现单点事件
        adapter.setOnItemClickListener(new NotesAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(int x, boolean adro, List<Notes> list1) {//这里将所选位置一个状态穿了过来
                if (adro) {
                    Log.e(TAG, "OnItemClick: x = " + x);
                    deleteList.add(x);//如果为选中状态，则添加
                } else {
                    //否则，删除当前list中的选中值
                    for (int i = 0; i < deleteList.size(); i++) {
                        if (deleteList.get(i) == x) {
                            deleteList.remove(i);
                        }
                    }
                }
                Collections.sort(deleteList);//从小到大对list排序，为了后面删除的方便
            }

        });
        //实现点击浮出的删除后进行删除
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //遍历整个list，如果位置为0直接删除，否则用当前list的值减去之前删除的个数再删除
                //因为当你删除一个值以后会更新整个recyclerview，这时他的长度发生改变而你存储的删除位置却没变，会导致错删，因为之前我们对其排过序所以没有先删大后删小的情况
                for (int i = 0; i < deleteList.size(); i++) {
                    if (i == 0) adapter.removeNotes(deleteList.get(i));
                    else adapter.removeNotes(deleteList.get(i) - i);
                }
                deleteList.removeAll(deleteList);//清空表（完成一次删除后立刻清空list表）
            }
        });
    }
}